﻿using System;
using System.Collections.Generic;
using System.Data.SQLite;
using System.Linq;
using System.Windows.Forms;
using Sunlight.Dispatch.Dal;
using Sunlight.Dispatch.Model;
using Sunlight.Dispatch.Tools;
using Sunlight.Dispatch.UI.Controls;
using EditState = Sunlight.Dispatch.UI.Extensions.EditState;

namespace Sunlight.Dispatch.UI {
    public partial class BusinessInsertFrm : BaseEditFrm {
        public BusinessInsertFrm(EditState editState, TBusiness business) {
            InitializeComponent();

            this.EditState = editState;
            this.Tag = 0;
            if(editState != EditState.新增 && business != null) {
                this.tbCode.ReadOnly = true;
                this.Tag = business.ID;
                this.tbCode.Text = business.Code;
                this.tbName.Text = business.Name;
                return;
            }
            this.tbCode.ReadOnly = false;
            this.Load += BusinessInsertFrm_Load;
        }

        private void BusinessInsertFrm_Load(object sender, EventArgs e) {
            this.InitComboBoxForDataBase();
        }

        private void InitComboBoxForDataBase() {
            var config = Utils.DeSerialize();
            if(config == null) {
                return;
            }
            using(var conn = new SQLiteConnection(string.Format($"Data Source={config.SqliteStoryPath};Version=3;"))) {
                conn.Open();
                var sqlExecute = "select Id,Name,ConnectString from ConnectStringConfigTb";
                var command = new SQLiteCommand(sqlExecute, conn);
                var reader = command.ExecuteReader();
                var source = new List<ConnectStringConfigTb>();
                while(reader.Read()) {
                    source.Add(new ConnectStringConfigTb {
                        Id = reader[0].ToString(),
                        Name = reader[1].ToString(),
                        ConnectString = reader[2].ToString()
                    });
                }

                //新增状态的接口配置，不允许编辑，必须在配置规则的情况下可以使用
                this.comboBoxDataBase.DataSource = null;
                this.comboBoxDataBase.DataSource = source;
                this.comboBoxDataBase.DisplayMember = "Name";
                this.comboBoxDataBase.ValueMember = "ConnectString";
            }
        }

        private void btnSave_Click(object sender, EventArgs e) {
            if(string.IsNullOrEmpty(this.tbCode.Text)) {
                MessageBoxEx.Show(this, "编号不能为空，请输入编号", "提示");
                return;
            }
            if(string.IsNullOrEmpty(this.tbName.Text)) {
                MessageBoxEx.Show(this, "名称不能为空，请输入名称", "提示");
                return;
            }
            if(comboBoxDataBase.SelectedItem == null) {
                MessageBoxEx.Show(this, "数据库名称不能为空，请输选择", "提示");
                return;
            }
            var db = comboBoxDataBase.SelectedItem as ConnectStringConfigTb;
            if(db == null)
                return;
            try {
                if(this.EditState == EditState.新增) {
                    using(var dataContext = new MonitorSettingDataContext(db.ConnectString)) {
                        dataContext.TBusiness.InsertOnSubmit(new TBusiness { Code = this.tbCode.Text, Name = this.tbName.Text });
                        dataContext.SubmitChanges();
                    }
                }

                if(this.EditState == EditState.编辑) {
                    var id = this.Tag as int?;
                    if(id.HasValue) {
                        using(var dataContext = new MonitorSettingDataContext(db.ConnectString)) {
                            var business = dataContext.TBusiness.FirstOrDefault(d => d.ID == id);
                            if(business != null) {
                                business.Name = this.tbName.Text;
                                dataContext.SubmitChanges();
                            }
                        }

                    }
                }
                this.RasieEditSubmitted();
            } catch(Exception exception) {
                MessageBoxEx.Show(exception.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
}
